﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GeneralAdaptiveFramework
{
    public partial class CollectForm : Form
    {
        readonly int INTERVAL = 1000;
        public UpdateSensor updateSensor;
        public static bool isStart;

        EZ_B.Camera camera;
        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
        public static EZ_B.CameraDetection.ColorDetection.ColorEnum color = EZ_B.CameraDetection.ColorDetection.ColorEnum.Red;
        int objectSize = 0;
        int brightness = 0;
        EZ_B.ObjectLocation objectLocation = null;

        public CollectForm()
        {
            isStart = false;
            InitializeComponent();
            ezB_Connect.EZB.ARDroneControl.OnImagecomplete += new EZ_B.ARDrone.DroneControl.OnImageCompleteHandler(ARDroneControl_OnImagecomplete);
        }

        private void loadForm()
        {
            Log("Connecting Drone...");
            //ezB_Connect.EZB.ShowDebugWindow();
                ezB_Connect.EZB.ARDroneControl.Connect();
            if (ezB_Connect.EZB.ARDroneControl.IsConnected)
            {
                Log("Connected.");
                camera = new EZ_B.Camera(ezB_Connect.EZB);
                camera_Selection();
            }

            collectTimer.Interval = INTERVAL;
            collectTimer.Enabled = true;
        }

        private void timerTick(object sender, EventArgs e)
        {
            if (!ezB_Connect.EZB.ARDroneControl.IsConnected)
                return;
            // check camera
            if (camera == null)
                return;
            if (!camera.IsActive)
                return;

            // execute
            stopWatch.Restart();
            objectSize = tbMinObjectSize.Value;
            brightness = tbBrightness.Value;
            objectLocation = camera.CameraBasicColorDetection.GetObjectLocationByColor(color, objectSize, (byte)brightness);

            // set value for collect component
            CollectInfo collectInfo = new CollectInfo();
            collectInfo.objectLocation.centerX = objectLocation.CenterX;
            collectInfo.objectLocation.centerY = objectLocation.CenterY;
            collectInfo.objectLocation.width = objectLocation.Rect.Width;
            collectInfo.objectLocation.height = objectLocation.Rect.Height;

            collectInfo.droneInfo.attitude = ezB_Connect.EZB.ARDroneControl.CurrentNavigationData.Altitude;
            collectInfo.droneInfo.battery = ezB_Connect.EZB.ARDroneControl.CurrentNavigationData.BatteryLevel;

            updateSensor(collectInfo);
            stopWatch.Stop();

            if (objectLocation != null)
                Log("Object found: {0} {1}", objectLocation.CenterX, objectLocation.CenterY);
                    

            // update timer
            
            stopWatch.Restart();
            camera.UpdatePreview();
            stopWatch.Stop();
        }

        void ARDroneControl_OnImagecomplete(Image image)
        {
            try
            {
                original.CreateGraphics().DrawImage(image, 0, 0, original.Width, original.Height);
            }
            catch
            {
            }
        }

        // change camera
        private void camera_Selection()
        {
            //string videoCaptureDeviceName = cameraType.SelectedItem.ToString();
            Log("Selected Camera: {0}", EZ_B.Camera.GetVideoCaptureDeviceList()[1]);

            string videoCaptureDeviceName = EZ_B.Camera.GetVideoCaptureDeviceList()[1];
            camera.StartCamera(videoCaptureDeviceName, recognition, original, 320, 240);
            Log("Video started: {0}", videoCaptureDeviceName);
        }

        /**
         * Print the message
         */
        private void Log(object txt, params object[] values)
        {
            logMessage.AppendText(string.Format(txt.ToString(), values));
            logMessage.AppendText(Environment.NewLine);
        }

        public void connect()
        {
            loadForm();
        }

        public void disconnect()
        {
            if (ezB_Connect.EZB.ARDroneControl.IsConnected == true)
                ezB_Connect.EZB.ARDroneControl.Disconnect();
            turnOffTimer();
        }

        public static void turnOffTimer()
        {
            collectTimer.Enabled = false;
        }

        public static bool getStart()
        {
            return isStart;
        }

        public static void setStart(bool start)
        {
            isStart = start;
        }

    }
}
